package com.plus.mybatisplus_demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plus.mybatisplus_demo.entity.Student;
import com.plus.mybatisplus_demo.service.StudentService;
import com.plus.mybatisplus_demo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    //分页带条件查询
    @GetMapping
    public ResultVo getPage(String stuName, @RequestParam(defaultValue = "1")Integer pageNum){
       Page page = studentService.getPage(stuName,pageNum);
       return ResultVo.success("",page);
    }

    //增加修改
    @PostMapping
    public ResultVo saveOrUpdate(@RequestBody Student student){
        try {
            studentService.saveOrUpdate(student);
            return ResultVo.success("操作成功",null);
        } catch (Exception e) {
            return ResultVo.error("操作失败");
        }

    }

    //删除
    @DeleteMapping("/{id}")
    public ResultVo del(@PathVariable Integer id){
       try {
           studentService.removeById(id);
           return ResultVo.success("删除成功",null);
       } catch (Exception e) {
           return ResultVo.error("删除失败");
       }
    }
}
